package models

import (
	"github.com/astaxie/beego/orm"
)

type Kline struct {
	Id                           int
	Symbol                       string
	Open_time                    int64
	Open                         float64
	High                         float64
	Low                          float64
	Close                        float64
	Volume                       float64
	Quote_asset_volume           float64
	Close_time                   int64
	Trades                       int64
	Taker_buy_base_asset_volume  float64
	Taker_buy_quote_asset_volume float64
	Ignore_                      float64
}

type Klinebtc struct {
	Id                           int
	Open_time                    int64
	Open                         float64
	High                         float64
	Low                          float64
	Close                        float64
	Volume                       float64
	Quote_asset_volume           float64
	Close_time                   int64
	Trades                       int64
	Taker_buy_base_asset_volume  float64
	Taker_buy_quote_asset_volume float64
	Ignore_                      float64
}

type Klineeth struct {
	Id                           int
	Open_time                    int64
	Open                         float64
	High                         float64
	Low                          float64
	Close                        float64
	Volume                       float64
	Quote_asset_volume           float64
	Close_time                   int64
	Trades                       int64
	Taker_buy_base_asset_volume  float64
	Taker_buy_quote_asset_volume float64
	Ignore_                      float64
}

type Klineltc struct {
	Id                           int
	Open_time                    int64
	Open                         float64
	High                         float64
	Low                          float64
	Close                        float64
	Volume                       float64
	Quote_asset_volume           float64
	Close_time                   int64
	Trades                       int64
	Taker_buy_base_asset_volume  float64
	Taker_buy_quote_asset_volume float64
	Ignore_                      float64
}

func init() {
	orm.RegisterModel(new(Kline), new(Klinebtc), new(Klineeth), new(Klineltc))
}

func GetKlineLastTime(symbol string) int64 {

	o1 := orm.NewOrm()
	var kline Kline
	o1.QueryTable("Kline").Filter("Symbol", symbol).OrderBy("-Id").One(&kline)
	return kline.Close_time
	//	if symbol == "BTC" {
	//		var kline Klinebtc
	//		o1.QueryTable("Klinebtc").OrderBy("-Id").One(&kline)
	//		return kline.Close_time
	//	} else if symbol == "ETH" {
	//		var kline Klineeth
	//		o1.QueryTable("Klineeth").OrderBy("-Id").One(&kline)
	//		return kline.Close_time
	//	} else if symbol == "LTC" {
	//		var kline Klineltc
	//		o1.QueryTable("Klineltc").OrderBy("-Id").One(&kline)
	//		return kline.Close_time
	//	} else {
	//		return 0
	//	}

}

func GetKlineData(symbol string, startTime, endTime int64) []Kline {
	o1 := orm.NewOrm()
	var kline []Kline
	o1.QueryTable("Kline").Filter("Symbol", symbol).Filter("Open_time__gte", startTime).Filter("Open_time__lt", endTime).Limit(-1).All(&kline)
	return kline
}
